function treeSelect(){
$("body").append('<div class="tree_mul_select_box"><div class="tree_mul_select">'+
		'<div class="tree_mul_select_head">选择<div class="tree_mul_select_close">×</div>'+
		'</div><ul class="easyui-tree tree_mul_select_list"></ul><br />'+
		'<div class="tree_mul_select_foot"><input class="tree_mul_select_check" type="button" value="确认选择" /></div></div></div>');
	mytree = $(".tree_mul_select_list");
	
	mytree.tree({
		checkbox: true,
		onlyLeafCheck: true
	})
	
	

}

treeSelect.prototype.bind = function(dom, datas){
	var This = this;
	var self = dom;
	var hide = self.next('input[type=hidden]');
	This.checkedArr(datas, function(initval){
		self.val(initval.textstr);
		hide.val(initval.idstr);
	});

	self.focus(function(){
		This.run(self, datas, function(result){
			self.val(result.textstr);
			hide.val(result.idstr);
		});
	})
}

treeSelect.prototype.checkedArr = function(datas, callback, childArr, resultArr){
	childArr = childArr?childArr:[];
	resultArr = resultArr?resultArr:[];
	var self = this;
	$.each(datas, function(key, val) {
		if(val.children && val.children[0]){
			childArr = childArr.concat(val.children);
		}else{
			if(val.checked) resultArr.push(val);
		}
	});
	
	if(childArr[0]){
		self.checkedArr(childArr, callback, [], resultArr);
	}else{
		var idarr = [];
		var textarr = [];
		$.each(resultArr, function(key, val) {
			idarr.push(val.id);
			textarr.push(val.text);
		});
		var result = {
			idstr: idarr.join(','),
			textstr: textarr.join(',')
		}
		if(callback) callback(result);
	}
}

treeSelect.prototype.run = function(_input, _data, callback){
	var self = this;
	var select_box = $(".tree_mul_select_box");
	var mytree = $(".tree_mul_select_list");
	var subBt = $(".tree_mul_select_check");
	var closeBt = $(".tree_mul_select_close");
	var cleanBt = $(".tree_mul_select_clean");
	mytree.tree({data:_data})
	select_box.show();
	subBt.click(function(){
		var nodes = mytree.tree('getChecked');
		var idArr = [];
		var textArr = [];
		$.each(nodes, function(key, val) {
			idArr.push(val.id);
			textArr.push(val.text);
		});
		var result = {
			idstr: idArr.join(','),
			textstr: textArr.join(',')
		}
		if(callback) callback(result);
		callback = null;
		select_box.hide();
	})
	
	$(".tree_mul_select_box").click(function(){
		$(this).hide();
		callback = null;
	})
	
	$(".tree_mul_select").click(function(e){
		var ev = e || event;
		ev.stopPropagation();
	})
	
	closeBt.click(function(){
		select_box.hide();
		callback = null;
	})
}
